package com.topscomm.main.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {
    @Bean
    public Docket appApi() {
        // 增加header
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> params = new ArrayList<Parameter>();
        parameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("Bearer ")
                .required(true)
                .build();
        params.add(parameterBuilder.build());
        // 配置规则
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("app")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.topscomm"))
                //去掉/cbo/开头api
//				.paths(PathSelectors.regex("^(?!\\/cbo\\/).*"))
                .paths(PathSelectors.ant("/app/**/*"))
                .build()
                .globalOperationParameters(params);
    }
    @Bean
    public Docket meetApi() {
        // 增加header
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> params = new ArrayList<Parameter>();
        parameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("Bearer ")
                .required(true)
                .build();
        params.add(parameterBuilder.build());
        // 配置规则
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("meet")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.topscomm"))
                .paths(PathSelectors.ant("/meet/**/*"))
                .build()
                .globalOperationParameters(params);
    }
    @Bean
    public Docket workApi() {
        // 增加header
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> params = new ArrayList<Parameter>();
        parameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("Bearer ")
                .required(true)
                .build();
        params.add(parameterBuilder.build());
        // 配置规则
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("work")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.topscomm"))
                .paths(PathSelectors.ant("/work/**/*"))
                .build()
                .globalOperationParameters(params);
    }
    @Bean
    public Docket cboApi() {
        // 增加header
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> params = new ArrayList<Parameter>();
        parameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("Bearer ")
                .required(true)
                .build();
        params.add(parameterBuilder.build());
        // 配置规则
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("cbo")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.topscomm"))
                .paths(PathSelectors.ant("/cbo/**/*"))
                .build()
                .globalOperationParameters(params);
    }


    /**
     * @description:api信息
     * @return
     * @author: xinfushuo
     * @date: 2019年3月16日上午10:23:51
     * @modify:
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("会议管理系统")
                .description("swagger2API")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

